package com.jvm.escape;

/**
 * EscapeAnalysis 逃逸分析 开启/与关闭 gc情况
 *
 * 栈上的空间一般而言是非常小的，只能存放若干变化和小的数据结构，无法存储大容量数据。
 * 目前的实现都是采用不那么准确但是时间压力相对较小的算法来完成逃逸分析，这就可能导
 * 致效果不稳定。所以，逃逸分析的效果只能在特定场景下，满足高频和高数量的小容量的变量
 * 分配结构，才是合适的。
 *
 *
 *
 * @author Lizhong
 * @date 2019/3/18
 */
public class EscapeAnalysis {

    private static class Foo {
        private int x;
        /**
         * 当打本行，Foo对象已经是大对象，尽管开启逃逸分析也会频繁gc
         */
        // private byte [] m= new byte[1024*2];
        private static int counter;

        public Foo() {
             x = (++counter);
        }
    }

    /**
     * -server -verbose:gc -XX:+DoEscapeAnalysis -Xmx10M -Xms10m
     * @param args
     */
    public static void main(String[] args) throws InterruptedException {
        System.out.println("start");
        for (int i = 0; i <1000000; ++i) {
            Foo foo = new Foo();
        }

        System.out.println(Foo.counter);
    }
    /**Out~
     * On
     * [0.157s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 4M->0M(10M) 1.661ms
     *
     * off
     *
     * [0.200s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 4M->0M(10M) 6.494ms
     * [0.203s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 3M->1M(10M) 1.091ms
     * [0.204s][info][gc] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 4M->1M(10M) 0.320ms
     * [0.205s][info][gc] GC(3) Pause Young (Normal) (G1 Evacuation Pause) 4M->0M(10M) 0.311ms
     * [0.206s][info][gc] GC(4) Pause Young (Normal) (G1 Evacuation Pause) 4M->0M(10M) 0.153ms
     * [0.207s][info][gc] GC(5) Pause Young (Normal) (G1 Evacuation Pause) 4M->0M(10M) 0.153ms
     * [0.209s][info][gc] GC(6) Pause Young (Normal) (G1 Evacuation Pause) 5M->0M(10M) 0.145ms
     * [0.210s][info][gc] GC(7) Pause Young (Normal) (G1 Evacuation Pause) 5M->0M(10M) 0.133ms
     * [0.211s][info][gc] GC(8) Pause Young (Normal) (G1 Evacuation Pause) 5M->0M(10M) 0.129ms
     * [0.213s][info][gc] GC(9) Pause Young (Normal) (G1 Evacuation Pause) 5M->0M(10M) 0.194ms
     * [0.214s][info][gc] GC(10) Pause Young (Normal) (G1 Evacuation Pause) 5M->0M(10M) 0.140ms
     * [0.215s][info][gc] GC(11) Pause Young (Normal) (G1 Evacuation Pause) 5M->0M(10M) 0.297ms
     * [0.216s][info][gc] GC(12) Pause Young (Normal) (G1 Evacuation Pause) 5M->0M(10M) 0.259ms
     * [0.220s][info][gc] GC(13) Pause Young (Normal) (G1 Evacuation Pause) 5M->1M(10M) 0.342ms
     * [0.221s][info][gc] GC(14) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.171ms
     * [0.222s][info][gc] GC(15) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.148ms
     * [0.223s][info][gc] GC(16) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.186ms
     * [0.225s][info][gc] GC(17) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.158ms
     * [0.226s][info][gc] GC(18) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.150ms
     * [0.227s][info][gc] GC(19) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.129ms
     * [0.228s][info][gc] GC(20) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.164ms
     * [0.231s][info][gc] GC(21) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.181ms
     * [0.245s][info][gc] GC(22) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 2.201ms
     * [0.247s][info][gc] GC(23) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.330ms
     * [0.249s][info][gc] GC(24) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.166ms
     * [0.250s][info][gc] GC(25) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.157ms
     * [0.252s][info][gc] GC(26) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.145ms
     * [0.253s][info][gc] GC(27) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.133ms
     * [0.254s][info][gc] GC(28) Pause Young (Normal) (G1 Evacuation Pause) 6M->1M(10M) 0.136ms
     * [0.256s][info][gc] GC(29) Pause Young (Normal) (G1 Evacuation Pause) 7M->1M(10M) 0.794ms
     * [0.258s][info][gc] GC(30) Pause Young (Normal) (G1 Evacuation Pause) 7M->1M(10M) 0.683ms
     * [0.260s][info][gc] GC(31) Pause Young (Normal) (G1 Evacuation Pause) 7M->1M(10M) 0.108ms
     */
}